package org.zky.modules.merchant.service.impl;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.zky.modules.merchant.domain.ZkyActivityTab;
import org.zky.modules.merchant.repository.ZkyActivityTabRepository;
import org.zky.modules.merchant.service.ZkyActivityTabService;
import org.zky.modules.merchant.service.dto.ZkyActivityTabDto;
import org.zky.modules.merchant.service.dto.ZkyActivityTabQueryCriteria;
import org.zky.modules.merchant.service.mapper.ZkyActivityTabMapper;
import org.zky.utils.FileUtil;
import org.zky.utils.PageUtil;
import org.zky.utils.QueryHelp;
import org.zky.utils.ValidationUtil;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

// 默认不使用缓存

/**
* @author chenglong
* @date 2020-03-23
*/
@Service
//@CacheConfig(cacheNames = "zkyActivityTab")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class ZkyActivityTabServiceImpl implements ZkyActivityTabService {

    private final ZkyActivityTabRepository zkyActivityTabRepository;

    private final ZkyActivityTabMapper zkyActivityTabMapper;

    public ZkyActivityTabServiceImpl(ZkyActivityTabRepository zkyActivityTabRepository, ZkyActivityTabMapper zkyActivityTabMapper) {
        this.zkyActivityTabRepository = zkyActivityTabRepository;
        this.zkyActivityTabMapper = zkyActivityTabMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(ZkyActivityTabQueryCriteria criteria, Pageable pageable){
        Page<ZkyActivityTab> page = zkyActivityTabRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(zkyActivityTabMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<ZkyActivityTabDto> queryAll(ZkyActivityTabQueryCriteria criteria){
        return zkyActivityTabMapper.toDto(zkyActivityTabRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public ZkyActivityTabDto findById(Long activityId) {
        ZkyActivityTab zkyActivityTab = zkyActivityTabRepository.findById(activityId).orElseGet(ZkyActivityTab::new);
        ValidationUtil.isNull(zkyActivityTab.getActivityId(),"ZkyActivityTab","activityId",activityId);
        return zkyActivityTabMapper.toDto(zkyActivityTab);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public ZkyActivityTabDto create(ZkyActivityTab resources) {
        return zkyActivityTabMapper.toDto(zkyActivityTabRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(ZkyActivityTab resources) {
        ZkyActivityTab zkyActivityTab = zkyActivityTabRepository.findById(resources.getActivityId()).orElseGet(ZkyActivityTab::new);
        ValidationUtil.isNull( zkyActivityTab.getActivityId(),"ZkyActivityTab","id",resources.getActivityId());
        zkyActivityTab.copy(resources);
        zkyActivityTabRepository.save(zkyActivityTab);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Long[] ids) {
        for (Long activityId : ids) {
            zkyActivityTabRepository.deleteById(activityId);
        }
    }

    @Override
    public void download(List<ZkyActivityTabDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ZkyActivityTabDto zkyActivityTab : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put(" activityName",  zkyActivityTab.getActivityName());
            map.put("1.online, 2.offline", zkyActivityTab.getActivityType());
            map.put("pictures", zkyActivityTab.getPicture());
            map.put("1:active 2:inactive 3:end", zkyActivityTab.getStatus());
            map.put(" startTime",  zkyActivityTab.getStartTime());
            map.put(" endTime",  zkyActivityTab.getEndTime());
            map.put(" province",  zkyActivityTab.getProvince());
            map.put(" city",  zkyActivityTab.getCity());
            map.put(" region",  zkyActivityTab.getRegion());
            map.put(" address",  zkyActivityTab.getAddress());
            map.put("activity all user number", zkyActivityTab.getQuotaNumber());
            map.put("has joined number", zkyActivityTab.getJoinNumber());
            map.put(" content",  zkyActivityTab.getContent());
            map.put(" remark",  zkyActivityTab.getRemark());
            map.put("delete time", zkyActivityTab.getDeleteAt());
            map.put(" createTime",  zkyActivityTab.getCreateTime());
            map.put(" updateTime",  zkyActivityTab.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}